package com.twu.biblioteca.ui;

import com.twu.biblioteca.ui.menus.AbstractMenu;

import java.util.Scanner;

/**
 * @author Alvi Jonathan Sait
 */
public class BibliotecaAppRunner {

    private final AbstractMenu mainMenu;

    public BibliotecaAppRunner(AbstractMenu mainMenu) {
        this.mainMenu = mainMenu;
    }

    public void run() {
        displayWelcomeMessage();

        AbstractMenu menu = mainMenu;

        while (menu != null) {
            display("========================================");
            display(menu.getOutput());
            display("========================================");
            display(menu.getUserPrompt());
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            menu = menu.handleInput(input);
            if (menu != null) {
                clearConsole();
            }
        }
    }

    private void displayWelcomeMessage() {
        display("****************************************");
        display("*                                      *");
        display("*        Welcome to Biblioteca.        *");
        display("*                                      *");
        display("****************************************");
    }

    private void display(String message) {
        System.out.println(message);
    }

    private void clearConsole() {
        for (int i = 0; i < 100; i++) {
            display("");
        }
    }
}
